<template>
  <div class="user-edit">
    <!-- Drawer抽屉 -->
    <Drawer
        :title="title"
        v-model="visible"
        width="720"
        draggable
        :mask-closable="type == '0'"
    >
      <div :style="{ maxHeight: maxHeight }" class="drawer-content">
        <Form label-colon v-show="type != '2'">
          <Row :gutter="32">
            <Col span="12">
              <FormItem label="上新ID">
                {{ form.id }}
              </FormItem>
            </Col>
          </Row>
        </Form>
        <Form
            ref="form"
            :model="form"
            :rules="formValidate"
            label-position="top"
        >
          <Row :gutter="32">
            <Col span="24">
              <FormItem label="发布内容">
                <Input type="textarea" v-model="form.content" :rows="4" />
              </FormItem>
            </Col>
          </Row>
          <Row :gutter="32">
            <Col span="24">
              <FormItem label="发布图片">
                <upload-pic-thumb v-model="pics"></upload-pic-thumb>
              </FormItem>
            </Col>
          </Row>
        </Form>
      </div>
      <div class="drawer-footer br" v-show="type != '0'">
        <Button type="primary" :loading="submitLoading" @click="submit"
        >提交</Button
        >
        <Button @click="visible = false">取消</Button>
      </div>
    </Drawer>
  </div>
</template>

<script>
import { addOrEditTopic } from "@/api/new";
import uploadPicThumb from "@/views/my-components/xboot/upload-pic-thumb";
export default {
  name: "new",
  components: {
    uploadPicThumb
  },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
    },
    type: {
      type: String,
      default: "0",
    },
  },
  data() {
    return {
      roleList: [],
      visible: this.value,
      title: "",
      submitLoading: false,
      maxHeight: 510,
      form: {

      },
      pics: [],
      formValidate: {
        // 表单验证规则
        content: [
          { required: true, message: "请输入发布内容", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(valid);
          console.log(this.type)
          this.form.goodPics = this.pics.join(',');
          if (this.type == "1") {
            console.log('编辑')
            // 编辑
            let data = {
              id: this.form.id,
              content: this.form.content,
              userId: JSON.parse(localStorage.getItem('userInfo')).id,
              pics: ''
            }
            this.submitLoading = true;
            addOrEditTopic(data).then((res) => {
              this.submitLoading = false;
              if (res.code === 200) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            });
          } else {
            // 添加

            this.submitLoading = true;
            addOrEditTopic(this.form).then((res) => {
              this.submitLoading = false;
              if (res.code === 200) {
                this.$Message.success("操作成功");
                this.$emit("on-submit", true);
                this.visible = false;
              }
            });
          }
        }
      });
    },
    setCurrentValue(value) {
      if (value === this.visible) {
        return;
      }
      if (this.type == "1") {
        this.title = "编辑";
        this.maxHeight = Number(document.documentElement.clientHeight - 121) + "px";
      } else if (this.type == "2") {
        this.title = "添加";
        this.maxHeight = Number(document.documentElement.clientHeight - 121) + "px";
      } else {
        this.title = "详情";
        this.maxHeight = "100%";
      }
      // 清空数据
      this.$refs.form.resetFields();
      if (this.type == "0" || this.type == "1") {
        // 回显数据
        let data = this.data;
        console.log(this.data);
        // 回显
        this.form = data;
      } else {
        // 添加
        // console.log(JSON.parse(localStorage.getItem('userInfo'))
        this.form = {
          content: '',
          userId: JSON.parse(localStorage.getItem('userInfo')).id,
        };
      }
      this.visible = value;
    },

    changeTime(e) {
      this.form.goodTime = e;
    }
  },
  watch: {
    value(val) {
      this.setCurrentValue(val);
    },
    visible(value) {
      this.$emit("input", value);
    },
  },
  mounted() {
  },
};
</script>

<style lang="less">
@import "../../../styles/drawer-common.less";
</style>